package com.gy.bearhelps.news.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gy.bearhelps.R;
import com.gy.bearhelps.news.bean.NewsItemBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * 作者：gy on 16/11/2
 * 说明：
 */
public class NewsItemAdapter extends BaseAdapter {

    private Context context;
    private List<NewsItemBean> lists;

    public NewsItemAdapter(Context context,List<NewsItemBean> lists){
        this.context = context;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if(view == null){
            view = View.inflate(context, R.layout.adapter_news_item,null);
            holder = new ViewHolder();
            holder.imgTop = (ImageView) view.findViewById(R.id.adapter_news_item_img_top);
            holder.imgIcon = (ImageView) view.findViewById(R.id.adapter_news_item_img_icon);
            holder.tvName = (TextView) view.findViewById(R.id.adapter_news_item_tv_name);
            holder.tvTime = (TextView) view.findViewById(R.id.adapter_news_item_tv_time);
            holder.llContent = (LinearLayout) view.findViewById(R.id.adapter_news_item_ll_content);
            holder.line = view.findViewById(R.id.adapter_news_item_line);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if(position == 0){
            if(TextUtils.isEmpty(lists.get(position).focusImg)){//焦点图
                holder.imgTop.setVisibility(View.GONE);
                holder.llContent.setVisibility(View.VISIBLE);
                if(TextUtils.isEmpty(lists.get(position).indexImg)){
                    holder.imgIcon.setVisibility(View.GONE);
                } else {
                    holder.imgIcon.setVisibility(View.VISIBLE);
                    ImageLoader.getInstance().displayImage(lists.get(position).indexImg,holder.imgIcon);
                }
                holder.tvName.setText(lists.get(position).title);
                holder.tvTime.setText(lists.get(position).releaseDate+"");
            } else {
                holder.imgTop.setVisibility(View.VISIBLE);
                holder.llContent.setVisibility(View.GONE);
                ImageLoader.getInstance().displayImage(lists.get(position).focusImg,holder.imgTop);
            }
        } else {
            holder.imgTop.setVisibility(View.GONE);
            holder.llContent.setVisibility(View.VISIBLE);

            if(TextUtils.isEmpty(lists.get(position).indexImg)){
                holder.imgIcon.setVisibility(View.GONE);
            } else {
                holder.imgIcon.setVisibility(View.VISIBLE);
                ImageLoader.getInstance().displayImage(lists.get(position).indexImg,holder.imgIcon);
            }
            holder.tvName.setText(lists.get(position).title);
            holder.tvTime.setText(lists.get(position).releaseDate+"");
        }

        return view;
    }

    class ViewHolder{
        ImageView imgTop,imgIcon;
        TextView tvName,tvTime;
        LinearLayout llContent;
        View line;
    }
}
